// -*- c++ -*-

//
// StreamFlowC : A C++ coding style to describe hardware structure for stream processing
//

// Copyright (c) 2009 the handy-eda-utils developer(s).
// Distributed under the MIT License.
// (See accompanying file COPYING or copy at
//  http://www.opensource.org/licenses/mit-license.php.)

template <typename T, int N>
module fbbuf {
 param:
  int chunk;		// output "chunk" data upon trigger input
  int initialstock;	// pretend that the buffer holds "initialstock" garbage items upon reset
 input:
  trig_t t;
  T      a;
 output:
  T      y;
};

struct fbbuf {
  T   m_arr[N];
  int m_wix, m_rix;
};

fbbuf() {
  m_wix = initialstock % N;
  m_rix = 0;
}

fbbuf(a) {
  m_arr[m_wix++] = a;
  if (m_wix == N)  m_wix = 0;
}

fbbuf(t) {
  for (int i=0; i<m_chunk; ++i) {
    y_out(m_arr[m_rix++]);
    if (m_rix == N)  m_rix = 0;
  }
}
